package com.example.springbootvue.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootvue.common.Result;
import com.example.springbootvue.entity.Book;
import com.example.springbootvue.entity.News;
import com.example.springbootvue.mapper.BookMapper;
import com.example.springbootvue.mapper.NewsMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Resource
    NewsMapper newsMapper;

    @PostMapping //新增
    public Result<?> save(@RequestBody News news){
        news.setTime(new Date());
        newsMapper.insert(news);
        System.out.println(news);
        return Result.success();
    }

    @GetMapping     //查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){

        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(News::getTitle,search);
        }
        Page<News> bookPage = newsMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);//::=get(可以直接访问属性)
        return Result.success(bookPage);
    }

    @PutMapping    //更新
    public Result<?> update(@RequestBody News news){
        newsMapper.updateById(news);

        return Result.success();
    }

    @DeleteMapping("/{id}")    //删除
    public Result<?> delete(@PathVariable long id){
        newsMapper.deleteById(id);
        return Result.success();
    }




}
